﻿using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Coder.Object2Report;
using Coder.Workflow;
using Fee.Orders;
using Fee.Orders.DesignWorkflow;
using Text.Helper;

namespace Fee.IO.Exporters
{
    public class DelegateOrderWorkActivityExportor : BaseExportor<WorkActivity>
    {
        private readonly IDictionary<string, DelegateOrderDetail> cache =
            new ConcurrentDictionary<string, DelegateOrderDetail>();

        protected override string SheetName => "数据";

        protected override Report<WorkActivity> CreateReportStructure(RenderBase render)
        {
            //站点名称	工单号	执行	审批建议

            var result = new Report<WorkActivity>(render);
            result.Column("工单号", f => ((BudgetOrder)f.ProcessInstance).Number);
            result.Column("站点名称", f => ((BudgetOrder)f.ProcessInstance).ConstructionTask.Station.Code);
            result.Column("站点类型",
                f => ((BudgetOrder)f.ProcessInstance).ConstructionTask.Station.Type.GetEnumDisplayName());
            result.Column("站点地区", f => ((BudgetOrder)f.ProcessInstance).ConstructionTask.Station.District.Name);


            result.Column("管道设计", f => GetVendorName(f, ContractType.Design, ConstructionType.Pipeline) ?? "");
            result.Column("管道监理", f => GetVendorName(f, ContractType.Supervisor, ConstructionType.Pipeline) ?? "");
            result.Column("管道施工", f => GetVendorName(f, ContractType.Construction, ConstructionType.Pipeline) ?? "");

            result.Column("专线设计", f => GetVendorName(f, ContractType.Design, ConstructionType.Cable) ?? "");
            result.Column("专线监理", f => GetVendorName(f, ContractType.Supervisor, ConstructionType.Cable) ?? "");
            result.Column("专线施工", f => GetVendorName(f, ContractType.Construction, ConstructionType.Cable) ?? "");

            result.Column("设备设计", f => GetVendorName(f, ContractType.Design, ConstructionType.Equipment) ?? "");
            result.Column("设备监理", f => GetVendorName(f, ContractType.Supervisor, ConstructionType.Equipment) ?? "");
            result.Column("设备施工", f => GetVendorName(f, ContractType.Construction, ConstructionType.Equipment) ?? "");

            result.Column("管道设计框架合同", f => GetContract(f, ContractType.Design, ConstructionType.Pipeline));
            result.Column("管道监理框架合同", f => GetContract(f, ContractType.Supervisor, ConstructionType.Pipeline));
            result.Column("管道施工框架合同", f => GetContract(f, ContractType.Construction, ConstructionType.Pipeline));

            result.Column("专线设计框架合同", f => GetContract(f, ContractType.Design, ConstructionType.Cable));
            result.Column("专线监理框架合同", f => GetContract(f, ContractType.Supervisor, ConstructionType.Cable));
            result.Column("专线施工框架合同", f => GetContract(f, ContractType.Construction, ConstructionType.Cable));

            result.Column("设备设计框架合同", f => GetContract(f, ContractType.Design, ConstructionType.Equipment));
            result.Column("设备监理框架合同", f => GetContract(f, ContractType.Supervisor, ConstructionType.Equipment));
            result.Column("设备施工框架合同", f => GetContract(f, ContractType.Construction, ConstructionType.Equipment));

            //00000000000000
            result.Column("管道设计项目", f => GetProject(f, ContractType.Design, ConstructionType.Pipeline));
            result.Column("管道监理项目", f => GetProject(f, ContractType.Supervisor, ConstructionType.Pipeline));
            result.Column("管道施工项目", f => GetProject(f, ContractType.Construction, ConstructionType.Pipeline));
            result.Column("专线设计项目", f => GetProject(f, ContractType.Design, ConstructionType.Cable));
            result.Column("专线监理项目", f => GetProject(f, ContractType.Supervisor, ConstructionType.Cable));
            result.Column("专线施工项目", f => GetProject(f, ContractType.Construction, ConstructionType.Cable));
            result.Column("设备设计项目", f => GetProject(f, ContractType.Design, ConstructionType.Equipment));
            result.Column("设备监理项目", f => GetProject(f, ContractType.Supervisor, ConstructionType.Equipment));
            result.Column("设备施工项目", f => GetProject(f, ContractType.Construction, ConstructionType.Equipment));

            result.Column("执行", f => "同意");
            result.Column("审批建议", f => "同意");
            return result;
        }

        private DelegateOrderDetail GetDetail(WorkActivity f, ContractType contractpeType,
            ConstructionType constructionType)
        {
            var key = contractpeType + "_" + constructionType;
            if (cache.ContainsKey(key)) return cache[key];
            var details = ((BudgetOrder)f.ProcessInstance).Details;
            var r = from detail in details
                    where detail.ContractType == contractpeType &&
                          detail.ConstructionType == constructionType
                    select detail;
            var result = r.FirstOrDefault();
            cache.Add(key, result);
            return result;
        }

        private string GetVendorName(WorkActivity f, ContractType contractType,
            ConstructionType constructionType)
        {
            var detial = GetDetail(f, contractType, constructionType);
            return detial?.Contract?.Vendor?.Name;
        }

        private string GetContract(WorkActivity f, ContractType contractpeType,
            ConstructionType constructionType)
        {
            var detial = GetDetail(f, contractpeType, constructionType);
            return detial.Contract.No;
        }

        private string GetProject(WorkActivity f, ContractType contractpeType,
            ConstructionType constructionType)
        {
            var detial = GetDetail(f, contractpeType, constructionType);
            return detial.Project.No;
        }
    }
}